package jp.co.kayo.android.twittertimeline;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TimelineAdapter extends ArrayAdapter<Tweet> {
	LayoutInflater inflater;
	SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 (E) HH:mm:ss");

	public TimelineAdapter(Context context, List<Tweet> objects) {
	    super(context, 0, objects);
	    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
		Tweet tweet = getItem(position);
		ViewHolder holder = null;
		if(convertView == null){
			convertView = inflater.inflate(R.layout.row, null);
			holder = new ViewHolder();
			holder.text = (TextView)convertView.findViewById(R.id.textView1);
			holder.created_at = (TextView)convertView.findViewById(R.id.textView2);
			convertView.setTag(holder);
		}
		else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(Utils.getCreatedAt(tweet.getCreated_at()));
		cal.add(Calendar.HOUR, 9);
		
		holder.text.setText(tweet.getText());
		holder.created_at.setText(format.format(cal.getTime()));
		
		return convertView;
    }

	public class ViewHolder{
		TextView text;
		TextView created_at;
	}
}
